package com.doucat.support.component.recycle.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.doucat.support.component.recycle.holder.BaseRecyclerHolder;
import com.doucat.support.component.recycle.holder.ItemRecyclerHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * File: BaseRecyclerAdapter.java
 * Author: win7
 * Version: V100R001C01
 * Create: 2021/1/26 11:04
 * Changes (from 2021/1/26)
 * -----------------------------------------------------------------
 * 2021/1/26 : Create BaseRecyclerAdapter.java (win7);
 * -----------------------------------------------------------------
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> {
    protected String TAG=getClass().getSimpleName();
    public Context context;
    public List<T> items;

    public BaseRecyclerAdapter(Context context) {
        this.context = context;
        this.items=new ArrayList<>();
    }

    public void setItems(List<T> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    public BaseRecyclerAdapter(Context context, List<T> data) {
        this.context = context;
        this.items = data == null ? new ArrayList<T>() : data;
    }

    @Override
    public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(getLayoutRes(),parent,false);
        BaseRecyclerHolder holder = new ItemRecyclerHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {
        holder.itemView.setFocusable(true);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(null,holder.itemView,position,holder.itemView.getId());
                }
            }
        });
    }
    protected abstract int getLayoutRes();
    @Override
    public int getItemCount() {
        return items.size();
    }

    public T getItem(int position){
        return items.get(position);
    }

    protected ItemClickListener onItemClickListener;

    public void setOnItemClickListener(ItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
